<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <!-- 引入react核心库 -->
  <script src="../js/react.development.js"></script>
  <!-- 引入react-dom,用于支持react操作DOM -->
  <script src="../js/react-dom.development.js"></script>
  <!-- 引入babel,用于将jsx转换为js -->
  <script src="../js/babel.min.js"></script>

  <body>
    <!-- 容器 -->
    <div id="app"></div>
  </body>

  <script type="text/babel">
    class Life extends React.Component {
      state = { opacity: 1 };

      death = () => {
        //卸载组件
        ReactDOM.unmountComponentAtNode(document.getElementById("app"));
      };

      //组件挂载完毕
      componentDidMount() {
        this.timer = setInterval(() => {
          let { opacity } = this.state;
          opacity -= 0.1;
          if (opacity <= 0) opacity = 1;
          this.setState({ opacity });
        }, 200);
      }

      //组件将要卸载
      componentWillUnmount() {
        clearInterval(this.timer);
      }

      render() {
        return (
          <div>
            <h1 style={{ opacity: this.state.opacity }}>React学不会了</h1>
            <button onClick={this.death}>不学了</button>
          </div>
        );
      }
    }

    ReactDOM.render(<Life />, document.getElementById("app"));
  </script>
</html>
